/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

using EnvDTE;
using EnvDTE80;

using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;

namespace DslEditorPowerToy.Recipes.Actions
{
    /// <summary>
    /// Sets the CustomTool property of a project item
    /// </summary>
    public class SetProjectItemCustomToolAction : ActionBase
    {
        private EnvDTE.ProjectItem _projectItem = null;
        private string _targetFileName = string.Empty;
        private string _customTool = string.Empty;
        private string _customToolNamespace = string.Empty;

        #region Constructors
        public SetProjectItemCustomToolAction()
            : base()
        {
        }
        #endregion

        #region Public Properties
        [Input(Required=true)]
        [CLSCompliant(false)]
        public EnvDTE.ProjectItem InputProjectItem
        {
            get
            {
                return this._projectItem;
            }
            set
            {
                this._projectItem = value;
            }
        }
        [Input(Required=true)]
        public string TargetFileName
        {
            get
            {
                return this._targetFileName;
            }
            set
            {
                this._targetFileName = value;
            }
        }
        [Input(Required=true)]
        public string CustomTool
        {
            get
            {
                return this._customTool;
            }
            set
            {
                this._customTool = value;
            }
        }
        [Input(Required=false)]
        public string CustomToolNamespace
        {
            get
            {
                return this._customToolNamespace;
            }
            set
            {
                this._customToolNamespace = value;
            }
        }
        #endregion

        #region IAction Members

        public override void Execute()
        {
            base.Execute();
            ValidateParameters();

            if (null == this._projectItem.ProjectItems)
                return;

            //Find item or subitem
            ProjectItem fileItem = DteHelper.FindItemByName(this._projectItem.ProjectItems, this._targetFileName, true);
            if (null == fileItem)
                return;

            //Set Custom Tool and Namespace
            fileItem.Properties.Item("CustomTool").Value = this._customTool;
            if (!(string.IsNullOrEmpty(this._customToolNamespace)))
                fileItem.Properties.Item("CustomToolNamespace").Value = this._customToolNamespace;
        }

        public override void Undo()
        {
            //Do undo action, or explain why you can't or should not
        }

        #endregion

        #region Protected Methods
        /// <summary>
        /// Validates parameters before execution
        /// </summary>
        protected override void ValidateParameters()
        {
            if (null == this._projectItem)
                throw new ArgumentNullException("InputProjectItem");
            if (null == this._targetFileName)
                throw new ArgumentNullException("TargetFileName");
            if (0 == this._targetFileName.Length)
                throw new ArgumentOutOfRangeException("TargetFileName");
            if (null == this._customTool)
                throw new ArgumentNullException("CustomTool");
            if (0 == this._customTool.Length)
                throw new ArgumentOutOfRangeException("CustomTool");
        }
        #endregion
    }
}
